package com.hengtongxing.hejiayun.life;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseFragment;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.GoodsListBean;
import com.hengtongxing.hejiayun.bean.MenuListBean;
import com.hengtongxing.hejiayun.bean.ShopCartListBean;
import com.hengtongxing.hejiayun.event.LoginEvent;
import com.hengtongxing.hejiayun.homepage.CleanServiceActivity;
import com.hengtongxing.hejiayun.homepage.ConsultServiceActivity;
import com.hengtongxing.hejiayun.homepage.PayFeeActivity;
import com.hengtongxing.hejiayun.homepage.RepairsServiceActivity;
import com.hengtongxing.hejiayun.homepage.adapter.HomeGoodsListAdapter;
import com.hengtongxing.hejiayun.homepage.adapter.HomeMenuListAdapter;
import com.hengtongxing.hejiayun.homepage.model.HomePageModelImpl;
import com.hengtongxing.hejiayun.homepage.model.IHomePageModel;
import com.hengtongxing.hejiayun.shop.ShopCartActivity;
import com.hengtongxing.hejiayun.shop.ShopGoodsDetailsActivity;
import com.hengtongxing.hejiayun.shop.model.ShopModelImpl;
import com.hengtongxing.hejiayun.utils.Dp2Px;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 生活
 */
public class LifeFragment extends BaseFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.ll_cart)
    RelativeLayout llCart;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.tv_cart_num)
    TextView tvCartNum;
    @BindView(R.id.ll_cart_num)
    RelativeLayout llCartNum;
    @BindView(R.id.ll_life_1)
    LinearLayout llLife1;
    @BindView(R.id.ll_life_2)
    LinearLayout llLife2;
    @BindView(R.id.ll_life_3)
    LinearLayout llLife3;
    @BindView(R.id.ll_life_4)
    LinearLayout llLife4;
    @BindView(R.id.ll_life_5)
    LinearLayout llLife5;
    @BindView(R.id.ll_life_6)
    LinearLayout llLife6;
    @BindView(R.id.ll_life_7)
    LinearLayout llLife7;
    @BindView(R.id.ll_life_8)
    LinearLayout llLife8;
    @BindView(R.id.recyclerView_menu)
    RecyclerView recyclerViewMenu;
    private HomeGoodsListAdapter listAdapter;
    private List<GoodsListBean.DataBean.ItemListBean> beanList;
    private IHomePageModel homePageModel;
    private HomeMenuListAdapter menuListAdapter;
    private List<MenuListBean.DataBean> menuBeanList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_life, container, false);
        ButterKnife.bind(this, view);
        registerEventBus();
        initView();
        requestShopGoodsIndex();
        requestAppMenu();
        return view;
    }

    @Subscribe
    public void loginEvent(LoginEvent event) {
        requestShopGoodsIndex();
    }

    @Override
    public void initImmersionBar() {
        //黑
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new HomeGoodsListAdapter(beanList);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int i = parent.getChildLayoutPosition(view) % 3;//每行2个
                switch (i) {
                    case 0://第一个
                        outRect.left = (int) Dp2Px.dp2Px(9);
                        outRect.right = (int) Dp2Px.dp2Px(9);
                        break;
                    case 1://第二个
                        outRect.left = (int) Dp2Px.dp2Px(9);
                        outRect.right = (int) Dp2Px.dp2Px(9);
                        break;
                    case 3://第二个
                        outRect.left = (int) Dp2Px.dp2Px(9);
                        outRect.right = (int) Dp2Px.dp2Px(9);
                        break;
                }
            }
        });
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener(position -> {
            Intent intent = new Intent(mActivity, ShopGoodsDetailsActivity.class);
            intent.putExtra("goods_id", beanList.get(position).getId() + "");
            startActivity(intent);
        });
        requestShopCartShow();

        menuBeanList = new ArrayList<>();
        menuListAdapter = new HomeMenuListAdapter(menuBeanList);
        recyclerViewMenu.setNestedScrollingEnabled(false);
        recyclerViewMenu.setLayoutManager(new GridLayoutManager(mActivity, 4));
        recyclerViewMenu.setAdapter(menuListAdapter);
        menuListAdapter.setOnCallBackListener(position -> {
            MenuListBean.DataBean dataBean = menuBeanList.get(position);
            if (dataBean.getPath().contains("||")) {
                String[] str = dataBean.getPath().split("\\|\\|");
                if ("ShopGoodsClass".equals(str[0])) {
                    Intent intent = new Intent(mActivity, GoodsListActivity.class);
                    intent.putExtra("query_str", str[1]);
                    startActivity(intent);
                }
            } else {
                if ("ShopGoodsClass".equals(dataBean.getPath())) {
                    Intent intent = new Intent(mActivity, GoodsListActivity.class);
                    intent.putExtra("query_str", "");
                    startActivity(intent);
                }
                if ("HousesRepair".equals(dataBean.getPath())) {
                    skipToActivity(RepairsServiceActivity.class);
                }
                if ("Advisory".equals(dataBean.getPath())) {
                    skipToActivity(ConsultServiceActivity.class);
                }
                if ("Housekeeping".equals(dataBean.getPath())) {
                    skipToActivity(CleanServiceActivity.class);
                }
                if ("PropertyCosts".equals(dataBean.getPath())) {
                    skipToActivity(PayFeeActivity.class);
                }
            }
        });

    }

    public void requestAppMenu() {
        homePageModel.requestAppMenu("2", new DataCallBack<MenuListBean>() {
            @Override
            public void onSuccessful(MenuListBean bean) {
                menuBeanList.clear();
                if (bean.getData() != null && bean.getData().size() > 0) {
                    menuBeanList.addAll(bean.getData());
                }
                menuListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 商品列表
     */
    public void requestShopGoodsIndex() {
        homePageModel.requestShopGoodsIndex(1, 1, "", new DataCallBack<GoodsListBean>() {
            @Override
            public void onSuccessful(GoodsListBean bean) {
                beanList.clear();
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                }
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 购物车 列表
     */
    public void requestShopCartShow() {
        new ShopModelImpl().requestIntegralShopCartShow(new DataCallBack<ShopCartListBean>() {
            @Override
            public void onSuccessful(ShopCartListBean bean) {
                if (bean.getData() != null && bean.getData().getItemList().size() > 0) {
                    tvCartNum.setText(bean.getData().getItemList().size() + "");
                    llCartNum.setVisibility(View.VISIBLE);
                } else {
                    tvCartNum.setText("");
                    llCartNum.setVisibility(View.GONE);
                }

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        requestShopCartShow();
    }

    private boolean isViewCreated; // 界面是否已创建完成
    private boolean isVisibleToUser; // 是否对用户可见

    /**
     * Fragment 创建期间setUserVisibleHint()方法是在onActivityCreated()之前被调用
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        tryLoadData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewCreated = true;
        tryLoadData();
    }

    public void tryLoadData() {
        if (isViewCreated && isVisibleToUser) {
            requestShopCartShow();
        }
    }

    @OnClick({R.id.iv_search, R.id.tv_more, R.id.ll_cart, R.id.ll_life_1, R.id.ll_life_2, R.id.ll_life_3, R.id.ll_life_4, R.id.ll_life_5, R.id.ll_life_6, R.id.ll_life_7, R.id.ll_life_8})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
            case R.id.tv_more:
                skipToActivity(GoodsListActivity.class);
                break;
            case R.id.ll_cart:
                skipToActivity(ShopCartActivity.class);
                break;
            case R.id.ll_life_1:
            case R.id.ll_life_2:
            case R.id.ll_life_3:
            case R.id.ll_life_4:
            case R.id.ll_life_5:
            case R.id.ll_life_6:
            case R.id.ll_life_7:
            case R.id.ll_life_8:
                skipToActivity(GoodsListActivity.class);
                break;
        }
    }
}
